package com.kly.user.serviceImpl;

import com.amazonaws.util.StringUtils;
import com.fqgj.common.api.Response;
import com.kly.service.UserVasPurchasesService;
import com.kly.user.constant.RedisKey;
import com.kly.user.db.dao.UserDao;
import com.kly.user.db.dao.UserEvaluateRewardDao;
import com.kly.user.db.entity.UserEntity;
import com.kly.user.db.entity.UserEvaluateRewardEntity;
import com.kly.user.service.AsyncSendMsgService;
import com.kly.user.service.UserEvaluateRewardService;
import com.kly.user.utils.DateWeekUtils;
import com.kly.user.utils.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service("userEvaluateRewardService")
public class UserEvaluateRewardServiceImpl implements UserEvaluateRewardService {


    @Resource
    private RedisService redisService;
    @Resource
    private UserEvaluateRewardDao userEvaluateRewardDao;
    @Resource
    UserDao userDao;
    @Resource
    private UserVasPurchasesService userVasPurchasesService;
    @Resource
    private AsyncSendMsgService asyncSendMsgService;

    @Override
    public Response<Integer> evaluateReward(String userCode) {
        int result = 0;
        int flashChatCount = 1;
        Long number = redisService.getNumber(RedisKey.FLASH_CHAT_COUNT);
        if (number != null) {
            flashChatCount = Math.toIntExact(number);
        }
        //判断是否有flash chat下发次数
        if (flashChatCount > 0) {
            //渠道是否开启
            boolean channelOpen = false;
            UserEntity userEntity = userDao.selectByUserCode(userCode);
            if (userEntity != null) {
                String channel = userEntity.getChannel();
                if (StringUtils.isNullOrEmpty(channel)) {
                    channel = "other";
                }
                Double score = redisService.zScore(RedisKey.EVALUATE_CHANNEL_SWITCH, channel);
                if (score != null && score == 1) {
                    channelOpen = true;
                }
            }
            if (channelOpen) {
                //判断当周奖励是否有下发
                Date mondayDate = DateWeekUtils.currentWeekMonday();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                int mondayDateInt = Integer.parseInt(sdf.format(mondayDate));
                int userEvaluateRewardCount = userEvaluateRewardDao.userEvaluateRewardCount(userCode, mondayDateInt);
                if (userEvaluateRewardCount == 0) {
                    //弹出提示
                    result = 1;
                }
            }
        }
        return Response.ok(result);
    }

    @Override
    public void addReward(String userCode) {
        int flashChatCount = 1;
        Long number = redisService.getNumber(RedisKey.FLASH_CHAT_COUNT);
        if (number != null) {
            flashChatCount = Math.toIntExact(number);
        }
        //赠送次数
        int result = userVasPurchasesService.updateUsageCount(flashChatCount, userCode, 1);
        if (result > 0) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            int dateInt = Integer.parseInt(sdf.format(new Date()));
            UserEvaluateRewardEntity userEvaluateRewardEntity = new UserEvaluateRewardEntity();
            userEvaluateRewardEntity.setUserCode(userCode);
            userEvaluateRewardEntity.setRewardDate(dateInt);
            userEvaluateRewardDao.insert(userEvaluateRewardEntity);
        }
    }

    @Override
    public void sendMessage(String userCode) {
        String message = "Congratulations.\n" +
                "Your reward has been issued. Go find someone to talk to and enjoy the day!\uD83E\uDD70";
        final UserEntity userEntity = userDao.selectByUserCode(userCode);
        asyncSendMsgService.sendMsg(userEntity.getAppCode(), userCode, "RC:InfoNtf", message, message);
    }
}
